package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vacation-work-mgr/cache"
	"vacation-work-mgr/model"
)

func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_type := c.PostForm("good_type")

	data := model.Good{
		GoodCode: good_code,
		GoodName: good_name,
		GoodType: good_type,
	}

	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func GoodOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))

	data := model.GoodOrder{
		OrderCode:  order_code,
		OrderName:  order_name,
		OrderPrice: order_price,
	}

	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func GoodOrderList(c *gin.Context) {

	//SELECT
	//good_orders.id as OrderId,
	//goods.good_code as GoodCode,
	//goods.good_name as GoodName,
	//goods.good_type as good_type,
	//good_orders.order_code as OrderCode,
	//good_orders.order_name as OrderName,
	//good_orders.order_price as OrderPrice
	//FROM
	//goods
	//LEFT JOIN good_orders ON good_orders.gid = goods.id

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var goodlist []model.GoodOrderList

	redis_key := "good_order_page_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		model.DB.
			Table("good_orders").
			Select(
				"good_orders.id as OrderId",
				"goods.good_code as GoodCode",
				"goods.good_name as GoodName",
				"goods.good_type as good_type",
				"good_orders.order_code as OrderCode",
				"good_orders.order_name as OrderName",
				"good_orders.order_price as OrderPrice",
			).
			Offset(offset).
			Limit(size).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Debug().
			Find(&goodlist)

		jsonData, _ := json.Marshal(goodlist)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodlist)
	}

	var total int64
	model.DB.Model(&model.GoodOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  500,
		"msg":   "列表展示成功",
		"data":  goodlist,
		"total": total,
	})

}

func GoodOrderDetail(c *gin.Context) {
	//SELECT
	//good_orders.id as OrderId,
	//goods.good_code as GoodCode,
	//goods.good_name as GoodName,
	//goods.good_type as good_type,
	//good_orders.order_code as OrderCode,
	//good_orders.order_name as OrderName,
	//good_orders.order_price as OrderPrice
	//FROM
	//goods
	//LEFT JOIN good_orders ON good_orders.gid = goods.id
	//WHERE good_orders.id = 1

	id := c.Query("id")

	var goodlists model.GoodOrderList

	redis_key := "good_order_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()

	if cacheData == "" {
		model.DB.
			Table("good_orders").
			Select(
				"good_orders.id as OrderId",
				"goods.good_code as GoodCode",
				"goods.good_name as GoodName",
				"goods.good_type as good_type",
				"good_orders.order_code as OrderCode",
				"good_orders.order_name as OrderName",
				"good_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN goods ON good_orders.gid = goods.id").
			Where("good_orders.id = ?", id).
			Debug().
			Find(&goodlists)

		jsonData, _ := json.Marshal(goodlists)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)

	} else {
		json.Unmarshal([]byte(cacheData), &goodlists)
	}

	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "列表展示成功",
		"data": goodlists,
	})

}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var list []model.GoodOrder

	redis_key := "list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()

	if cacheData == "" {
		model.DB.Offset(offset).Limit(size).Find(&list)

		jsonData, _ := json.Marshal(list)

		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64

	model.DB.Model(&model.GoodOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "展示成功",
		"data":  list,
		"total": total,
	})
	return

}
